/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.table;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.table.AbstractTableModel;

import com.pearreview.application.Messages;
import com.pearreview.util.review.Meeting;
import com.pearreview.util.review.Protocol;

public class MeetingCheckBoxTableModel extends AbstractTableModel {
	private static final long serialVersionUID = -2080487666784231133L;
	private String[] columns = {
			Messages.getString(
					"PearReview.Moderator.invitations.meeting.select"), 
			Messages.getString(
					"PearReview.Moderator.invitations.meeting.date"), 
			Messages.getString(
					"PearReview.Moderator.invitations.meeting.location"),
			Messages.getString(
					"PearReview.Moderator.invitations.meeting.protocol")};
	
	private List<Meeting> meetings;
	private Set<Integer> selectedIndizes = new HashSet<Integer>();
	
	public void setMeetings(List<Meeting> meetings) {
		this.meetings = meetings;
		selectedIndizes.clear();
		fireTableDataChanged();
	}
	
	public void clearSelection() {
		selectedIndizes.clear();
		fireTableRowsUpdated(0, getRowCount() - 1);
	}
	
	public void selectAll() {
		for(int i = 0 ; i < getRowCount() ; i++) {
			selectedIndizes.add(i);
		}
		fireTableRowsUpdated(0, getRowCount());
	}
	
	public int getSelectedCount() {
		return selectedIndizes.size();
	}
	
	public List<Meeting> getSelectedMeetings() {
		List<Meeting> selectedMeetings = new ArrayList<Meeting>();
		
		for(Integer index: selectedIndizes) {
			selectedMeetings.add(meetings.get(index));
		}
		
		return selectedMeetings;
	}
	
	public List<Meeting> getMeetings() {
		return meetings;
	}
	
	public void setSelectedMeeting(Meeting selectedMeeting) {
		int index = meetings.indexOf(selectedMeeting);
		if(index >= 0) {
			selectedIndizes.add(index);
			
			fireTableRowsUpdated(index, index);
		}
	}
	
	public void setSelectedMeetings(List<Meeting> selectedMeetings) {
		for(Meeting selectedMeeting: selectedMeetings) {
			setSelectedMeeting(selectedMeeting);
		}
	}

	@Override
	public int getColumnCount() {
		return columns.length;
	}
	
	@Override
	public String getColumnName(int column) {
		return columns[column];
	}

	@Override
	public int getRowCount() {
		return meetings == null ? 0 : meetings.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Meeting meeting = meetings.get(rowIndex);
		
		switch(columnIndex) {
		case 0:
			return selectedIndizes.contains(rowIndex) ? true : false;
		case 1:
			return meeting.getPlannedDate();
		case 2:
			return meeting.getPlannedLocation();
		case 3:
			return meeting.getProtocol();
		}
		
		return null;
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch(columnIndex) {
		case 0:
			return Boolean.class;
		case 1:
			return Calendar.class;
		case 2:
			return String.class;
		case 3:
			return Protocol.class;
		}
		
		return super.getColumnClass(columnIndex);
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return columnIndex == 0 ? true : false;
	}
	
	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		if(columnIndex == 0) {
			if(aValue.equals(true)) {
				selectedIndizes.add(rowIndex);
			} else {
				selectedIndizes.remove(rowIndex);
			}
			fireTableRowsUpdated(rowIndex, rowIndex);
		}
	}

}
